var sendBtn = document.getElementById('send');
var searchInput = document.getElementById('search');
var loginName = "test";


window.setInterval("queryMsg();",5000);



sendBtn.onclick = function(params) {
    if (searchInput.value) {
        renderDom('mine', searchInput.value);
        queryLoginNameWithSeriNo();
        sendMsg();
       // AJAX({
       //      method: 'get',
       //      url: 'sendMsg',
       //      data: {
       //          text: searchInput.value
       //      },
       //      success: function(res) {
       //          renderDom('robot', loginName+"说--->"+res.text);
       //      }
       //  });
        searchInput.value = '';
    }
}

function queryMsg(){
    $.ajax({
        url:"queryMsg",
        type:"post",
        async:false,
        data:{"text":searchInput.value},
        dataType: "json",
        success:function(respResult){
            if ("nodata" == respResult.text) {

            }else if ("nologin" == respResult.text) {

            }else{
                var list =respResult.text;
                for (var i=0;i<list.length;i++){
                    renderDom('robot', respResult.text[i]);
                }


            }
        },
        error:function(respResult){
            alert('异常回到首页');
            window.location.href="index";
        }
    });
}


function sendMsg(){
    $.ajax({
        url:"sendMsg",
        type:"get",
        async:false,
        data:{"text":searchInput.value},
        dataType: "json",
        success:function(respResult){
            // renderDom('robot', loginName+respResult.text);
        },
        error:function(respResult){
            alert('异常回到首页');
            window.location.href="index";
        }
    });
}
function queryLoginNameWithSeriNo(){
    $.ajax({
        url:"queryLoginNameWithSeriNo",
        type:"get",
        async:false,
        data:{"text":"queryLoginNameWithSeriNo"},
        /* dataType: "json", */
        success:function(respResult){
            if("nologin"==respResult){
                alert("要不你先登录下？");
                window.location.href="login";
            }else{
                loginName=respResult;
            }
        },
        error:function(respResult){
            alert('异常回到首页');
            window.location.href="index";
        }
    });

}


searchInput.onkeydown = function(e) {
    if (e.keyCode === 13) {
        sendBtn.onclick();
    }
}

/**
 * 
 * @param {string} who  代表谁说的话，那就创建谁的dom
 * @param {string} text 说话的内容
 */

function renderDom(who, text) {
    var content = document.getElementsByClassName('content')[0];
    var dom = document.createElement('div');
    dom.className = 'clearfix';
    dom.classList.add(who);
    dom.innerHTML = '<img src="static/img/' + who + '.jpg" alt="">';
    var textDom = document.createElement('div');
    textDom.className = 'text';
    textDom.innerText = text;
    dom.appendChild(textDom);
    content.appendChild(dom);
    // 设置滚动条滚动的距离  scrollHeight 获取的是当没有滚动条被自己的内容去撑开的时候内容的高度
    content.scrollTop = content.scrollHeight;
}